package com.nit.generate;

import com.nit.tools.loop.NitLoopTools;

import java.util.Random;

/**
 * 车牌号 生成器
 *
 * @Auth: 逆天开源-逍遥哥哥
 * @link: 1715656022@qq.com
 * @Date: 2023-07-21
 */
public class NitPlateGenerator {
    //省份简称
    private static final char[] PROVINCE_PREFIX = {
            '京', '津', '沪', '渝',
            '冀', '豫', '云', '辽', '黑', '湘', '皖', '鲁', '苏', '浙', '赣',
            '鄂', '甘', '晋', '陕', '吉', '闽', '贵', '粤', '青', '川', '琼',
            '宁', '新', '藏', '桂', '蒙',
            '港', '澳', '台'
    };
    private static final String[] ALPHAS = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    public static void main(String[] args) {
        NitLoopTools.loop(4, () -> {
            String tmp = NitPlateGenerator.generate();
            System.out.println("随机车牌号:" + tmp + "<br>");
        });
    }

    public static String generate() {
        return getPrefix() + getFive();
    }

    private static String getPrefix() {
        StringBuffer plateBuffer = new StringBuffer();
        plateBuffer.append(PROVINCE_PREFIX[NitNumberGenerator.generateInt(0, PROVINCE_PREFIX.length)]); // 注意：分开加，因为加的是2个char
        plateBuffer.append(ALPHAS[NitNumberGenerator.generateInt(0, ALPHAS.length)]);
        plateBuffer.append("·");
        return plateBuffer.toString();
    }

    public static String getFive() {
        int count = 0;
        String sb = "";
        // 随机获取后五位随机序号
        while (count < 5) {
            Random random = new Random();
            String str2 = ALPHAS[random.nextInt(ALPHAS.length)];
            int num = random.nextInt(10);
            int j = 0;
            // 控制字母和数字的数量
            if (count > 2) {
                for (int i = 0; i < sb.length(); i++) {
                    char c = sb.charAt(i);
                    if ((c >= 'A') && (c <= 'Z')) {
                        j++;
                    }
                }
            }
            if (j < 2) {
                // 字母与数字的概率相同
                if (random.nextBoolean()) {
                    sb += num;
                } else {
                    sb += str2;
                }
            } else {
                sb += num;
            }
            count++;
        }
        // 判断是否符合车牌规范，如果开头有连续的字母，则重新获取
        return sb.matches("^.*[A-Z]{2}.*$") ? getFive() : sb;
    }

}

